package com.medicine.hbp.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import org.kymjs.kjframe.KJBitmap;

import com.medicine.hbp.base.WebActivity;
import com.medicine.hbp.mob.AdlnfoVo;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.ui.BannerWebActivity;
import com.medicine.hbp.ui.HotspotActivity;
import com.medicine.hbp.ui.IntegralActivity;
import com.medicine.hbp.ui.MainActivity;
import com.medicine.hbp.ui.WonderfulAppActivity;
import com.medicine.hbp.R;

public final class ImageFragment extends Fragment {

	private static final String KEY_URL = "ImageFragment:url";
	private AdlnfoVo vo ;
	private ImageView imageView ;

	public static ImageFragment newInstance(AdlnfoVo vo) {
		ImageFragment fragment = new ImageFragment();
		fragment.vo = vo;
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_URL)) {
			vo = (AdlnfoVo) savedInstanceState.getSerializable(KEY_URL);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		imageView = new ImageView(getActivity());
		imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		imageView.setScaleType(ScaleType.FIT_XY);
		imageView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				/**
				 * 广告链接
				 * 16001  更多精彩应用
				 * 16101 近期热点1
				 * 16102 近期热点2
				 * 16103 近期热点3
				 * 16201 专题1
				 * 16202 专题2
				 * 16203 专题3
				 * 16204 专题4
				 * 16301 关心
				 * 16401 投票
				 * 16501 积分活动
				 */
				if(vo.adType==1){
					Intent intent=new Intent(getActivity(),BannerWebActivity.class);
					if(TextUtils.isEmpty(LoginSp.getToken(getActivity()))){
						intent.putExtra("URL",vo.adUrl);
					}else{
						intent.putExtra("URL",vo.adUrl+"&token="+LoginSp.getToken(getActivity()));
					}
					intent.putExtra("NAME",vo.adName);
					getActivity().startActivity(intent);
				}else{
					Intent intent=null;
					if(vo.adUrl.equals("16101")){//近期热点1
						intent=new Intent(getActivity(),HotspotActivity.class);
						intent.putExtra("id", "1");
						intent.putExtra("name", "近期热点1");
						intent.putExtra("type", 1);
					}else if(vo.adUrl.equals("16102")){//近期热点2
						intent=new Intent(getActivity(),HotspotActivity.class);
						intent.putExtra("id", "2");
						intent.putExtra("name", "近期热点2");
						intent.putExtra("type", 1);
					}else if(vo.adUrl.equals("16103")){//近期热点3
						intent=new Intent(getActivity(),HotspotActivity.class);
						intent.putExtra("id", "3");
						intent.putExtra("name", "近期热点3");
						intent.putExtra("type", 1);
					}else if(vo.adUrl.equals("16201")){//专题1
						intent=new Intent(getActivity(),HotspotActivity.class);
						intent.putExtra("id", "1");
						intent.putExtra("name", "专题1");
						intent.putExtra("type", 2);
					}else if(vo.adUrl.equals("16202")){//专题2
						intent=new Intent(getActivity(),HotspotActivity.class);
						intent.putExtra("id", "2");
						intent.putExtra("name", "专题2");
						intent.putExtra("type", 2);
					}else if(vo.adUrl.equals("16203")){//专题3
						intent=new Intent(getActivity(),HotspotActivity.class);
						intent.putExtra("id", "3");
						intent.putExtra("name", "专题3");
						intent.putExtra("type", 2);
					}else if(vo.adUrl.equals("16204")){//专题4
						intent=new Intent(getActivity(),HotspotActivity.class);
						intent.putExtra("id", "4");
						intent.putExtra("name", "专题4");
						intent.putExtra("type", 2);
					}else if(vo.adUrl.equals("16001")){//更多精彩应用
						intent=new Intent(getActivity(),WonderfulAppActivity.class);
					}else if(vo.adUrl.equals("16301")){//关心
						MainActivity.setTab(1);
					}else if(vo.adUrl.equals("16401")){//投票
						MainActivity.setTab(2);
					}else if(vo.adUrl.equals("16501")){//积分活动
						intent=new Intent(getActivity(),IntegralActivity.class);
					}
					if(intent!=null){
						getActivity().startActivity(intent);
					}
				}

			}
		});
		LinearLayout layout = new LinearLayout(getActivity());
		layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		layout.setGravity(Gravity.CENTER);
		layout.setBackgroundColor(getActivity().getResources().getColor(R.color.transparent));
		layout.addView(imageView);
		//   View layout  = inflater.inflate(R.layout.image, null);
		return layout;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if(vo.adImg!=null&&!"".equals(vo.adImg)){
//    		DisplayImageOptions options = new DisplayImageOptions.Builder()
//	        .showImageForEmptyUri(R.drawable.ceshi2) // resource or drawable
//	        .showImageOnFail(R.drawable.ceshi2) // resource or drawable
//	        .cacheInMemory(false)
//	        .cacheOnDisk(true)
//	        .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
//	        .bitmapConfig(Bitmap.Config.RGB_565)
//	        .build();
			KJBitmap bitmap=new KJBitmap();
			bitmap.display(imageView, vo.adImg);
//        	ImageLoader.getInstance().displayImage(url,imageView,options);
		}else{
			KJBitmap bitmap=new KJBitmap();
			bitmap.display(imageView, "http://img2.3lian.com/2014/f5/158/d/88.jpg");
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putSerializable(KEY_URL, vo);
	}

}
